﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cspl.AnimalFight.Lib.Data;
using Cspl.AnimalFight.Lib.EventHandlers;
using Cspl.AnimalFight.Lib.Interface;

namespace Cspl.AnimalFight.Lib
{
    public class BattleField
    {
        private IList<AnimalBase> _animals;

        public BattleField()
        {
            this._animals = new List<AnimalBase>();
        }

        public IList<AnimalBase> Animals
        {
            get { return _animals; }
            private set { _animals = value; }
        }
        
        public AnimalBase this[string name]
        {
            get { return this._animals.FirstOrDefault(a => a.AnimalName == name); }
            set
            {
                if (value != null)
                {
                    IAttack attackingAnimal = value as IAttack;
                    if (attackingAnimal != null)
                    {
                        attackingAnimal.AttackEvent += new AttackDelegate(attackingAnimal_AttackEvent);
                    }
                    this._animals.Add(value);
                }
                IDefense defendingAnimal = value as IDefense;
                if (defendingAnimal != null)
                {
                    defendingAnimal.defenseEvent += new DefenseDelegate(defendingAnimal_defenseEvent);
                }
                this._animals.Add(value);
            }
        }

        private void defendingAnimal_defenseEvent(object sender, DefenseEventHandlers e)
        {
            var defendingAnimal = this[e.AnimalName];

            if (defendingAnimal != null)
            {
                IDefense theAnimal = defendingAnimal as IDefense;
                if (theAnimal != null)
                {
                    theAnimal.IsDefending = true;
                }
                else
                {
                    Console.WriteLine("The animal {0} cannot defend itself.", e.AnimalName);
                }

            }
        }


        private void attackingAnimal_AttackEvent(object sender, AttackEventHandlers e)
        {
            var animalTarget = this[e.AnimalTargetName];

            if (animalTarget != null)
            {
                IAttackable attackedAnimal = animalTarget as IAttackable;
                if (attackedAnimal != null)
                {
                    Console.WriteLine("{0} is Attacked with AP = {1}.", e.AnimalTargetName, e.AP);
                    attackedAnimal.Attacked(e.AP);
                }
                else
                {
                    Console.WriteLine("The animal {0} is immortal.", e.AnimalTargetName);
                }
            }
            // Bug fixed by Ricky
            else
            {
                Console.WriteLine("Animal target not available.");
            }
        }

        public void Command(string animalName, Commands command, string targetName)
        {
            var animal = this[animalName];


            if (animal != null)
            {
                switch (command)
                {
                    case Commands.Attack:
                        if (animal is IAttack)
                        {
                            (animal as IAttack).Attack(targetName);
                        }
                        else
                        {
                            Console.WriteLine("Animal {0} cannot attack.", animalName);
                        }
                        break;
                    case Commands.Defense:
                        if (animal is IDefense)
                        {
                            (animal as IDefense).Defense();
                        }
                        else
                        {
                            Console.WriteLine("Animal {0} cannot defense.", animalName);
                        }
                        break;
                    case Commands.Escape:
                        animal.Escape();
                        break;
                }
            }
            else
            {
                Console.WriteLine("Animal {0} is not exist.", animalName);
            }
            
        }
    }

    public enum Commands { Attack, Defense, Escape }
}
